package com.example.fido2013;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class LoginScreen extends Activity {
		
	@Override	 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_login_screen);
	    
	    boolean notValidUser = false;
	    
	    Intent intent = getIntent();
	    if (intent != null) {
		    notValidUser = intent.getBooleanExtra("not valid user", false);
	    }
	    
	    if (notValidUser == true) {
	    	Toast.makeText(getBaseContext(), "You are not a valid user. Please contact a FIDO administrator to have your account authorized for FIDO access.", Toast.LENGTH_LONG).show();
	    }
	}
	
	/** Called when the user clicks the Send button */
	public void performLogin(View view) {	
		if (AreWeOnline(LoginScreen.this)) {
			Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://dev.m.gatech.edu/login?url=fido2013://loggedin&sessionTransfer=window"));
			startActivity(myIntent);
		}
		if (!AreWeOnline(LoginScreen.this)) {
			runOnUiThread(new Runnable() {
			    public void run() {
			    	Toast.makeText(getBaseContext(), "No network connectivity detected. Please make sure WiFi or 3G/4G data network is available.", Toast.LENGTH_LONG).show();
			    }
			});		        	
        }

	}
	
	public void goToBluetoothDirect(View view) {
		Intent i = new Intent(LoginScreen.this, DirectBluetoothMode.class);
		startActivity(i);		
	}
	
	public static boolean AreWeOnline(Context context) {
	      ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
	       
	      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
	      return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
	}
}
